package com.jizhangben.dao;

import com.jizhangben.pojo.AccountPojo;
import com.jizhangben.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AccountDaoImpl implements AccountDao {
    @Override
    public List<AccountPojo> getByAll(int uId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            // 1.获取连接
            connection = JdbcUtils.getConnection();
            //2.获取执行者对象
            preparedStatement = connection.prepareStatement("select * from account where userId= ? ");
            preparedStatement.setInt(1, uId);
            resultSet = preparedStatement.executeQuery();
            ArrayList<AccountPojo> accountPojos = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Double money = resultSet.getDouble("money");
                int userId = resultSet.getInt("userId");
                Date time = resultSet.getDate("time");
                String details = resultSet.getString("details");
                AccountPojo accountPojo = new AccountPojo(id, name, money, userId, time, details);
                // 存入到集合中
                accountPojos.add(accountPojo);
            }
            return accountPojos;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 释放资源
            JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
        }

    }
    public AccountPojo getByIdAcount(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            // 1.获取连接
            connection =JdbcUtils.getConnection();
            //2.获取执行者对象
            preparedStatement = connection.prepareStatement("select * from account where id=?");
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            if (!resultSet.next()) {
                return null;
            }
            // 连接到db查询数据
            String name = resultSet.getString("name");
            Double money = resultSet.getDouble("money");
            int userId = resultSet.getInt("userId");
            Date time = resultSet.getDate("time");
            String details = resultSet.getString("details");
            AccountPojo accountPojo = new AccountPojo(id, name, money, userId, time, details);
            return accountPojo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 释放资源
            JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
        }
    }
    public int insertAccount(AccountPojo accountPojo) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            // 1.获取连接
            connection = JdbcUtils.getConnection();
            // 开启事务
            JdbcUtils.beginTransaction(connection);
            //2.获取执行者对象
            preparedStatement = connection.prepareStatement("INSERT INTO `jizhangben`.`account`(`id`, `name`, `money`, `userid`, `time`, `details`) VALUES (null,?,?,?,?,?);");
            preparedStatement.setString(1, accountPojo.getName());
            preparedStatement.setDouble(2, accountPojo.getMoney());
            preparedStatement.setInt(3, accountPojo.getUserId());
            preparedStatement.setDate(4,accountPojo.getTime());
            preparedStatement.setString(5, accountPojo.getDetails());

            int result = preparedStatement.executeUpdate();
            // 代码执行没有问题的情况下 则直接提交事务
            JdbcUtils.commitTransaction(connection);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚事务
            JdbcUtils.rollBackTransaction(connection);
            return 0;
        } finally {
            // 释放资源
            JdbcUtils.closeConnection(preparedStatement, connection);
        }
    }
}
